package com.wd.lookcar.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.RadioGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.wd.buycar.view.activity.ContrastActivity
import com.wd.common.base.BaseFragment
import com.wd.common.bean.QuickSearchBean
import com.wd.common.viewmodel.MyViewModel
import com.wd.lookcar.R
import com.wd.lookcar.SearchActivity
import com.wd.lookcar.adapter.LookCarAdapter
import com.wd.lookcar.databinding.FragmentLookBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LookFragment : BaseFragment<FragmentLookBinding>() {
    private lateinit var list: ArrayList<QuickSearchBean.Result>
    private var price = ""
    private var grade = "轿车"
    private var rank = "order by id desc"
    private var size: Int = 5
    private val viewModel : MyViewModel by viewModels()
    private lateinit var adapter : LookCarAdapter
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ) = FragmentLookBinding.inflate(layoutInflater)

    @SuppressLint("NotifyDataSetChanged")
    override fun initData() {
        binding.lookTitle.title.text = "看车"
        binding.lookTitle.ivPlay.setOnClickListener{
            startActivity(Intent(context,ContrastActivity::class.java))
        }
        list = ArrayList<QuickSearchBean.Result>()
        //价格选择更改
        binding.carPrice.setOnCheckedChangeListener { _: RadioGroup, i: Int ->
            //修改价格条件
            when (i) {
                //价格不限
                R.id.price_irrespective -> price = ""
                //10万以下
                R.id.ten_less -> price = "<10"
                //10-20万
                R.id.ten_to_twenty -> price = "10,20"
                //20-30万
                R.id.twenty_to_thirty -> price = "20,30"
                //30-50万
                R.id.thirty_fifty -> price = "30,50"
            }
            //访问接口
            list.clear()
            viewModel.lookQuickSearch(price, grade, rank, 1, size)
            binding.lookXrecy.removeAllViews()
        }
        //级别选择更改
        binding.carLevelGroup.setOnCheckedChangeListener { _: RadioGroup, i: Int ->
            when (i) {
                //轿车
                R.id.car_sedan -> grade = "轿车"
                //suv
                R.id.car_suv -> grade = "SUV"
                //mpv
                R.id.car_mpv -> grade = "MPV"
                //新能源
                R.id.car_ev -> grade = "新能源"
                //跑车
                R.id.car_run -> grade = "跑车"
            }
            //访问接口
            list.clear()
            viewModel.lookQuickSearch(price, grade, rank, 1, size)
            binding.lookXrecy.removeAllViews()
        }
        //排序选择更改
        binding.carSortGroup.setOnCheckedChangeListener { _: RadioGroup, i: Int ->
            when (i) {
                //热度高
                R.id.sort_heat -> rank = "order by id desc"
                //销量高
                R.id.sort_sales -> rank = "order by id asc"
                //价格升序
                R.id.sort_price_up -> rank = "order by price asc"
                //价格降序
                R.id.sort_price_down -> rank = "order by price desc"
                //油耗
                R.id.sort_fuel -> rank = "order by id desc"
            }
            list.clear()
            viewModel.lookQuickSearch(price, grade, rank, 1, size)
            binding.lookXrecy.removeAllViews()
        }
        binding.moreCondition.setOnClickListener {
            startActivity(Intent(context,SearchActivity::class.java))
        }
        adapter = LookCarAdapter(requireContext(), list)
        binding.lookXrecy.layoutManager = LinearLayoutManager(context)
        binding.lookXrecy.adapter = adapter
        binding.carPrice.check(R.id.price_irrespective)
        viewModel.quickSearch.observe(this){
            it?.let {
                if(it.status == "0000"){
                    list.addAll(it.result)
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun event(bean : QuickSearchBean){
        list.clear()
        if(bean.status == "0000"){
            list.addAll(bean.result)
            adapter.notifyDataSetChanged()
        }
    }
    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        EventBus.getDefault().unregister(this)
        super.onStop()
    }

}